﻿using Pay.Core;
using Pay.DTO;
using Pay.IService;
using Pay.TO;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace Pay.Web.Areas.Admin.Controllers
{
    [RouteArea("admin")]
    [RoutePrefix("activity")]
    public class ActivityController : AdminController
    {
        private IActivityService _iActivityService;

        public ActivityController(IActivityService iActivityService)
        {
            _iActivityService = iActivityService;
        }

        public ActionResult Index()
        {
            return View();
        }

        public JsonResult Gets(ActivityQuery query)
        {
            query.curManagerId = currentUser.user_id;
            var model = _iActivityService.GetActivitys(query);
            return Json(ResultToJson.Json(new { data = model, e_activity_status_list = typeof(EActivityStatus).GetDropdownItemList() }), JsonRequestBehavior.AllowGet);
        }

        public JsonResult Get(long id)
        {
            activity_dto_ex model = new activity_dto_ex() { type = EActivityType.WorldCup, status = EActivityStatus.NotStarted, begin_time = DateTime.Now, end_time = DateTime.Now.AddDays(1) };//新增时也要获得活动列表下拉数据
            if (id > 0)
                _iActivityService.GetActivity(id).ToCopyProperty(model);
            model.activity_type_value = ((int)model.type).ToString();
            model.status_value = ((int)model.status).ToString();
            model.img_url = model.img_url ?? "";
            model.link_url = model.link_url ?? "";
            return Json(ResultToJson.Json(model), JsonRequestBehavior.AllowGet);
        }

        [HttpPost]
        [ValidateInput(false)]
        public JsonResult Add(activity_dto_ex dto)
        {
            dto.type = (EActivityType)int.Parse(dto.activity_type_value);
            dto.status = (EActivityStatus)int.Parse(dto.status_value);
            var file_url = Upload("Activity");
            if (!string.IsNullOrEmpty(file_url))
                dto.img_url = file_url;

            var result = _iActivityService.AddActivity(dto,currentUser.user_id);
            return Json(result, JsonRequestBehavior.AllowGet);

        }

        [HttpPost]
        [ValidateInput(false)]
        public JsonResult Update(activity_dto_ex dto)
        {
            dto.type = (EActivityType)int.Parse(dto.activity_type_value);
            dto.status = (EActivityStatus)int.Parse(dto.status_value);
            var file_url = Upload("Activity");
            if (!string.IsNullOrEmpty(file_url))
                dto.img_url = file_url;

            var result = _iActivityService.UpdateActivity(dto);
            return Json(result, JsonRequestBehavior.AllowGet);

        }

        [HttpPost]
        public JsonResult UpdateStatus(long id, int status)
        {
            var result = _iActivityService.UpdateActivityStatus(id, status);
            return Json(ResultToJson.Json(result > 0 ? " 修改成功" : "修改失败", result));
        }

        [HttpPost]
        public JsonResult UpdateIndexShow(long id, int isShow) {
            var result = _iActivityService.UpdateIndexShow(id, isShow);
            return Json(ResultToJson.Json(result ? " 修改成功" : "修改失败", result));
        }



    }
}